<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 上面按钮 -->
      <page-tools>
        <span slot="after">
          <el-button type="primary" small @click="addPermisson(1,'0')">添加权限</el-button>
        </span>
      </page-tools>
      <!-- 表格 -->
      <el-table border :data="list" row-key="id">
        <el-table-column label="名称" prop="name" />
        <el-table-column align="center" label="标识" prop="code" />
        <el-table-column align="center" label="描述" prop="description" />
        <el-table-column align="center" label="操作">
          <template slot-scope="{ row }">
            <el-button v-if="row.type === 1" type="text" @click="addPermisson(2,row.id)">添加</el-button>
            <el-button type="text" @click="editPermisson(row.id)">编辑</el-button>
            <el-button type="text" @click="delPermisson(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 放置一个弹层，新增或者删除 -->
      <el-dialog :title="showText" :visible="showDialog" @close="btnCancel">
        <el-form ref="perForm" :model="formData" :rules="rules">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" style="width:90%" />
          </el-form-item>
          <el-form-item label="开启">
            <el-switch
              v-model="formData.enVisible"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="btnCancel">取 消</el-button>
          <el-button type="primary" @click="btnOk">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getPermissonList, getPermissonDetail, addPermissonList, uploadPermisson, delPermisson } from '@/api/permisson'
import { tranListToTreeData } from '@/utils/index'
export default {
  data() {
    return {
      list: [], // 所有权限
      showDialog: false, // 控制弹层的显示与隐藏
      formData: {
        name: '',
        code: '',
        description: '',
        enVisible: '1',
        type: '',
        pid: ''
      },
      showText: '', // 弹层标题
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getPermissonList()
  },
  methods: {
    // 获取权限
    async getPermissonList() {
      const res = await getPermissonList()
      // console.log(res)
      // 将扁平化数据转换为树形数据
      this.list = tranListToTreeData(res, '0')
      // console.log(this.list)
    },
    // 添加权限
    addPermisson(type, pid) {
      this.formData.type = type
      this.formData.pid = pid
      this.showText = '添加权限点'
      this.showDialog = true
    },
    // 编辑权限
    async  editPermisson(id) {
      this.formData = await getPermissonDetail(id)
      this.showText = '编辑权限点'
      this.showDialog = true
    },
    // 点击确认
    async btnOk() {
      await this.$refs.perForm.validate()
      if (this.formData.id) {
        // 编辑
        await uploadPermisson(this.formData)
        this.$message.success('编辑成功')
      } else {
        // 新增
        await addPermissonList(this.formData)
        this.$message.success('新增成功')
      }
      this.getPermissonList()
      this.showDialog = false
    },
    // 点击取消
    btnCancel() {
      // 重置表单数据
      this.formData = {
        name: '',
        code: '',
        description: '',
        enVisible: '1',
        type: '',
        pid: ''
      }
      // 重置校验规则
      this.$refs.perForm.resetFields()
      // 关闭弹层
      this.showDialog = false
    },
    // 点击删除
    delPermisson(id) {
      this.$confirm('确认删除该权限点吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(async() => {
        await delPermisson(id)
        this.getPermissonList()
        this.$message.success('删除成功')
      }).catch(() => {
        this.$message.warning('已取消')
      })
    }
  }
}
</script>
<style>
</style>
